<?php


namespace api\web\controller;


use api\web\model\MessageModel;
use cmf\controller\RestBaseController;
use think\facade\Filesystem;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Exception;

class MessageController extends RestBaseController
{
//    //个人信息查询
//  public function message(){
//      $id = input('id/d');
//      $list = MessageModel::order('id','desc')
//          ->field('id,image,name,mobile',)
//          ->select($id);
//      $this->success('个人信息查询',$list);
//  }
//  //编辑
//  public function update(){
//      //接收
//      $data=input('post.');
//      //去除image数据
//      unset($data['image']);
//      //修改图片
//      $image = request()->file('image');
//      $result = Filesystem::disk('public')->putFile('MessageImage',$image);
//      $image2 =   request()->domain().'DIRECTORY_SEPARATOR'.'storage'.DIRECTORY_SEPARATOR.$result;
//
//      $ms = MessageModel::find($data['id']);
//      $ms->image=$image2;
//      $ms->name=$data['name'];
//      $ms->mobile=$data['mobile'];
//
//      if($ms->save()){
//          $this->success('保存成功',[
//              'msg'=>$data,$image2,
//          ]);
//      }else{
//          $this->error('保存失败');
//      }
//  }
    //查询个人信息

    public function getUserInfo()
    {
        $header = request()->header();
        if(!isset($header['token'])|| empty($header['token'])){
            $this -> error('缺少 token');
        }
        $key = new Key(config( 'app.jwt_key'),'HS256');
        try {
            $res = JWT::decode($header['token'],$key);
        }catch(Exception $exception){
            $this->error($exception->getMessage());
        }
        $userInfo = MessageModel::field(  'id,image,name,mobile')
            ->where('id',$res->data->user_id)
            ->find();
        $userInfo['image']=request()->domain() . '/storage/' . $userInfo['image'];
        $this->success( '用户信息', $userInfo);
    }
    //修改个人信息
    public function updateInfo(){

                $header = request()->header();
                if(!isset($header['token'])|| empty($header['token'])){
                    $this -> error('缺少 token');
                }
                $key = new Key(config( 'app.jwt_key'),'HS256');
                try {
                    $res = JWT::decode($header['token'],$key);
                }catch(Exception $exception){
                    $this->error($exception->getMessage());
        }

        $data = input('post.');
        //验证
        $user = MessageModel::find($res->data->user_id);
        $user->image = $data['image'];
        $user->name = $data['name'];
        $user->mobile = $data['mobile'];
        if($user->save()){
            $this->success('修改成功');
        }
        $this->error('修改失败');
    }
  //头像上传
//    public function upload(){
//        $id = input('id');
//        // 获取表单上传文件 例如上传了001.jpg
//        $file = request()->file('image');
//        // 上传到本地服务器
//        $savename = \think\facade\Filesystem::disk('public')->putFile( 'topic', $file,'md5');
//        $image2 =   request()->domain().DIRECTORY_SEPARATOR.'storage'.DIRECTORY_SEPARATOR.$savename;
//        $addImage = MessageModel::where('id','=',$id)->find();
//        $addImage->image = $image2;
//        if ($addImage->save()){
//            return json(['code' => 1, 'msg' => "上传成功" , 'data' => ['url' => $image2,]]);
//        }else{
//            return json(['code' => 0,'msg' => "上传失败" , 'data' => null]);
//        }
//    }
    //本地上传
    public function upload()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('image');
        $result = Filesystem::disk('public')->putFile('image', $file);
        // 上传到本地服务器
        if ($result) {
            $this->success('upload success', [
                'url' => $result
            ]);
        }
    }
}